﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class EmailSettingDA : IEmailSettingDA
    {
        private string ADDRESSEE = "@ADDRESSEE";
        private string CARBON_COPY = "@CARBON_COPY";
        private string CONTENT = "@CONTENT";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCRIPTION = "@DESCRIPTION";
        private string EMAIL_ACCOUNT = "@EMAIL_ACCOUNT";
        private string EMAIL_SETTING_ID = "@EMAIL_SETTING_ID";
        private string EMAL_PWD = "@EMAL_PWD";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_HTML = "@IS_HTML";
        private string IS_REGULAR_SEND = "@IS_REGULAR_SEND";
        private string REGULAR_TIME = "@REGULAR_TIME";
        private string REMARK = "@REMARK";
        private string SMTP = "@SMTP";
        private string SQL_CHECK_EMAIIL_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM @_@EMAIL_SETTING WHERE EMAIL_SETTING_ID = @EMAIL_SETTING_ID ";
        private string SQL_DELETE_EMAIIL_SETTING = " DELETE FROM @_@EMAIL_SETTING WHERE EMAIL_SETTING_ID = @EMAIL_SETTING_ID ";
        private string SQL_INSERT_EMAIIL_SETTING = " INSERT INTO @_@EMAIL_SETTING ( WH_ID, EMAIL_SETTING_ID, DESCRIPTION, SMTP, EMAIL_ACCOUNT, EMAL_PWD, ADDRESSEE, CARBON_COPY, IS_HTML, IS_REGULAR_SEND, REGULAR_TIME, CONTENT, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @EMAIL_SETTING_ID, @DESCRIPTION, @SMTP, @EMAIL_ACCOUNT, @EMAL_PWD, @ADDRESSEE, @CARBON_COPY, @IS_HTML, @IS_REGULAR_SEND, @REGULAR_TIME, @CONTENT, @REMARK, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_EMAIIL_SETTING = " SELECT  ROWID, WH_ID, EMAIL_SETTING_ID, DESCRIPTION, SMTP, EMAIL_ACCOUNT, EMAL_PWD, ADDRESSEE, CARBON_COPY, IS_HTML, IS_REGULAR_SEND, REGULAR_TIME, CONTENT, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@EMAIL_SETTING ";
        private string SQL_UPDATE_EMAIIL_SETTING = " UPDATE @_@EMAIL_SETTING SET WH_ID = @WH_ID, DESCRIPTION = @DESCRIPTION, SMTP = @SMTP, EMAIL_ACCOUNT = @EMAIL_ACCOUNT, EMAL_PWD = @EMAL_PWD, ADDRESSEE = @ADDRESSEE, CARBON_COPY = @CARBON_COPY, IS_HTML = @IS_HTML, IS_REGULAR_SEND = @IS_REGULAR_SEND, REGULAR_TIME = @REGULAR_TIME, CONTENT = @CONTENT, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE EMAIL_SETTING_ID = @EMAIL_SETTING_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckEmaiilSettingIDUnique(string emaiilSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_EMAIIL_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckEmaiilSettingIDUnique(DataBase dataBase, DbTransaction tran, string emaiilSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_EMAIIL_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteEmaiilSetting(List<string> emaiilSettingID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < emaiilSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteEmaiilSetting(DataBase dataBase, DbTransaction tran, List<string> emaiilSettingID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < emaiilSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllEmaiilSetting(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllEmaiilSetting(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public EmailSettingInfo GetEmaiilSettingByID(string emaiilsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID) + " WHERE EMAIL_SETTING_ID = @EMAIL_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilsettingID) };
            EmailSettingInfo emaiilSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    emaiilSettingInfo = new EmailSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["EMAIL_SETTING_ID"].ToString(), reader["DESCRIPTION"].ToString(), reader["SMTP"].ToString(), reader["EMAIL_ACCOUNT"].ToString(), reader["EMAL_PWD"].ToString(), reader["ADDRESSEE"].ToString(), reader["CARBON_COPY"].ToString(), reader["IS_HTML"].ToString(), reader["IS_REGULAR_SEND"].ToString(), (reader["REGULAR_TIME"].ToString() != "") ? DateTime.Parse(reader["REGULAR_TIME"].ToString()) : (CS00003 = new DateTime()), reader["CONTENT"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return emaiilSettingInfo;
        }

        public EmailSettingInfo GetEmaiilSettingByID(DataBase dataBase, DbTransaction tran, string emaiilsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID) + " WHERE EMAIL_SETTING_ID = @EMAIL_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@EMAIL_SETTING_ID", emaiilsettingID) };
            EmailSettingInfo emaiilSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                emaiilSettingInfo = new EmailSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["EMAIL_SETTING_ID"].ToString(), reader["DESCRIPTION"].ToString(), reader["SMTP"].ToString(), reader["EMAIL_ACCOUNT"].ToString(), reader["EMAL_PWD"].ToString(), reader["ADDRESSEE"].ToString(), reader["CARBON_COPY"].ToString(), reader["IS_HTML"].ToString(), reader["IS_REGULAR_SEND"].ToString(), (reader["REGULAR_TIME"].ToString() != "") ? DateTime.Parse(reader["REGULAR_TIME"].ToString()) : (CS00003 = new DateTime()), reader["CONTENT"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return emaiilSettingInfo;
        }

        public DataSet GetEmaiilSettingByQueryList(List<string> sqlWhere, EmailSettingQueryEntity emaiilsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere.ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere.ToString();
                }
            }
            if (!emaiilsettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, emaiilsettingQuery.CurrentPage, emaiilsettingQuery.PageSize, emaiilsettingQuery.SortField, emaiilsettingQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetEmaiilSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, EmailSettingQueryEntity emaiilsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_EMAIIL_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere.ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere.ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, emaiilsettingQuery.CurrentPage, emaiilsettingQuery.PageSize, emaiilsettingQuery.SortField, emaiilsettingQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertEmaiilSetting(EmailSettingInfo emaiilSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                emaiilSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_EmaiilSetting_Parameters(emaiilSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertEmaiilSetting(DataBase dataBase, DbTransaction tran, EmailSettingInfo emaiilSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                emaiilSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_EmaiilSetting_Parameters(emaiilSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_EmaiilSetting_Parameters(EmailSettingInfo emaiilSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.EMAIL_SETTING_ID, SqlDbType.VarChar, 100), new SqlParameter(this.DESCRIPTION, SqlDbType.VarChar, 200), new SqlParameter(this.SMTP, SqlDbType.VarChar, 50), new SqlParameter(this.EMAIL_ACCOUNT, SqlDbType.VarChar, 200), new SqlParameter(this.EMAL_PWD, SqlDbType.VarChar, 100), new SqlParameter(this.ADDRESSEE, SqlDbType.VarChar, 500), new SqlParameter(this.CARBON_COPY, SqlDbType.VarChar, 500), new SqlParameter(this.IS_HTML, SqlDbType.VarChar, 1), new SqlParameter(this.IS_REGULAR_SEND, SqlDbType.VarChar, 1), new SqlParameter(this.REGULAR_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.CONTENT, SqlDbType.VarChar, 0x3e8), new SqlParameter(this.REMARK, SqlDbType.VarChar, 0x3e8), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(emaiilSettingInfo.WhID))
            {
                paramArray[0].Value = emaiilSettingInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = emaiilSettingInfo.EmailSettingID;
            if (!string.IsNullOrEmpty(emaiilSettingInfo.Description))
            {
                paramArray[2].Value = emaiilSettingInfo.Description;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = emaiilSettingInfo.Smtp;
            paramArray[4].Value = emaiilSettingInfo.EmailAccount;
            paramArray[5].Value = emaiilSettingInfo.EmalPwd;
            if (!string.IsNullOrEmpty(emaiilSettingInfo.Addressee))
            {
                paramArray[6].Value = emaiilSettingInfo.Addressee;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.CarbonCopy))
            {
                paramArray[7].Value = emaiilSettingInfo.CarbonCopy;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.IsHtml))
            {
                paramArray[8].Value = emaiilSettingInfo.IsHtml;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.IsRegularSend))
            {
                paramArray[9].Value = emaiilSettingInfo.IsRegularSend;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (emaiilSettingInfo.RegularTime != new DateTime())
            {
                paramArray[10].Value = emaiilSettingInfo.RegularTime;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.Content))
            {
                paramArray[11].Value = emaiilSettingInfo.Content;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.Remark))
            {
                paramArray[12].Value = emaiilSettingInfo.Remark;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.IsActive))
            {
                paramArray[13].Value = emaiilSettingInfo.IsActive;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.CreatedBy))
            {
                paramArray[14].Value = emaiilSettingInfo.CreatedBy;
            }
            else
            {
                paramArray[14].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(emaiilSettingInfo.UpdatedBy))
            {
                paramArray[15].Value = emaiilSettingInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[15].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateEmaiilSetting(EmailSettingInfo emaiilSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                emaiilSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_EmaiilSetting_Parameters(emaiilSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateEmaiilSetting(DataBase dataBase, DbTransaction tran, EmailSettingInfo emaiilSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                emaiilSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_EmaiilSetting_Parameters(emaiilSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_EMAIIL_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

